package com.hzw.saas.web.app.old.storage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.old.storage.IGroupResourceService;
import com.hzw.saas.api.old.storage.bo.ResourceFileBO;
import com.hzw.saas.api.old.storage.enums.ResourceAuthEnum;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.service.old.storage.annotation.FileAuthBefore;
import com.hzw.saas.service.old.storage.annotation.GroupAuthBefore;
import com.hzw.saas.web.app.old.storage.param.ResourceShareParam;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;

/**
 * @author zzl
 * @since 12/30/2020
 */
// @Controller
// @RequestMapping("/group")
// @Validated
@AllArgsConstructor
// @Api(tags = "群组管理/群组资源")
public class GroupResourceController {

    private final IGroupResourceService groupResourceService;


    @GetMapping("/resource/{groupId}")
    @SysLog
    @GroupAuthBefore(groupId = "#groupId")
    @ApiOperation(value = "获取群组共享资源列表", notes = "获取群组中所有共享资料列表，包括本人分享的")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupId", value = "群组ID", dataType = "String", required = true)
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "请求成功"),
        @ApiResponse(code = 401, message = "请求非法，用户不属于该群组")
    })
    @ApiOperationSort(1)
    public ResponseEntity<IPage<ResourceFileBO>> listByGroup(@PathVariable String groupId, PageParam pageParam) {
        // 获取
        IPage<ResourceFileBO> resourceDtoIPage = groupResourceService.listGroupFileInfo(pageParam, groupId, null);
        return ResponseEntity.ok(resourceDtoIPage);
    }

    // @GetMapping("/user/resource/{groupId}")
    // @GroupRoleCheck(GroupRoleCheck.GroupRole.MEMBER)
    // @ApiOperation(value = "获取群组中本用户共享资源列表", notes = "获取群组中本用户共享资源列表")
    // @ApiImplicitParams({
    //     @ApiImplicitParam(name = "groupId", value = "群组ID", dataType = "String", required = true)
    // })
    // @ApiResponses({
    //     @ApiResponse(code = 200, message = "请求成功"),
    //     @ApiResponse(code = 401, message = "请求非法，用户不属于该群组")
    // })
    // @ApiOperationSort(2)
    public ResponseEntity<IPage<ResourceFileBO>> listByGroupAndUser(@PathVariable String groupId, PageParam pageParam) {
        SaasUser saasUser = SecurityUtils.getUser();
        IPage<ResourceFileBO> resourceDtoIPage = groupResourceService.listGroupFileInfo(pageParam, groupId, saasUser.getUserId());
        return ResponseEntity.ok(resourceDtoIPage);
    }

    @PostMapping("/share")
    @SysLog(operation = "分享文件到群组", mode = 3)
    @FileAuthBefore(fileIds = "#param.fileIds", auth = ResourceAuthEnum.OWN)
    @GroupAuthBefore(groupIds = "#param.groupIds")
    @ApiOperation(value = "分享文件到群组", notes = "分享多个文件到多个群组")
    @ApiResponses({
        @ApiResponse(code = 200, message = "请求成功，true=分享完成"),
        @ApiResponse(code = 401, message = "请求非法，该资源不属于本用户/资源不存在")
    })
    @ApiOperationSort(3)
    public ResponseEntity<Boolean> share(@Validated @RequestBody ResourceShareParam param) {
        SaasUser user = SecurityUtils.getUser();
        // 分享
        if (CollectionUtil.isNotEmpty(param.getGroupIds()) && CollectionUtil.isNotEmpty(param.getFileIds())) {
            for (String groupId : param.getGroupIds()) {
                for (String fileId : param.getFileIds()) {
                    groupResourceService.addGroupFile(groupId, user.getUserId(), fileId);
                }
            }
        }
        return ResponseEntity.ok(true);
    }

    @DeleteMapping("/share")
    @SysLog(operation = "取消分享文件", mode = 3)
    @FileAuthBefore(fileIds = "#param.fileIds", auth = ResourceAuthEnum.OWN)
    @GroupAuthBefore(groupIds = "#param.groupIds")
    @ApiOperation(value = "取消分享文件", notes = "取消分享多个文件到多个群组")
    @ApiResponses({
        @ApiResponse(code = 200, message = "请求成功，true=取消完成"),
        @ApiResponse(code = 401, message = "请求非法，该资源不属于本用户/资源不存在")
    })
    @ApiOperationSort(4)
    public ResponseEntity<Boolean> shareCancel(@Validated @RequestBody ResourceShareParam param) {
        SaasUser user = SecurityUtils.getUser();
        // 取消分享
        if (CollectionUtil.isNotEmpty(param.getGroupIds()) && CollectionUtil.isNotEmpty(param.getFileIds())) {
            for (String groupId : param.getGroupIds()) {
                for (String fileId : param.getFileIds()) {
                    groupResourceService.deleteGroupFile(groupId, user.getUserId(), fileId);
                }
            }
        }
        return ResponseEntity.ok(true);
    }

}
